﻿using Core.Toolkit.Core.Model;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text;

namespace Core.Toolkit.Core
{
    public static class ClaimsPrincipalExtension
    {

        public static UserIdentity GetUserIdentity(this ClaimsPrincipal claimsPrincipal)
        {
            UserIdentity ui = new UserIdentity();
            var sidClaim = claimsPrincipal.FindFirst(ClaimTypes.Sid);
            var nameIdentifierClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier);
            var factory = claimsPrincipal.FindFirst("factory");
            var headoffice = claimsPrincipal.FindFirst("headoffice");

            ui.Id = Guid.Parse(sidClaim.Value);
            ui.LoginName = nameIdentifierClaim?.Value;
            ui.Name = claimsPrincipal.Identity.Name;
            ui.Headoffice = true.ToString().ToUpper().Equals(headoffice?.Value?.ToString()?.ToUpper());
            if (factory != null)
                ui.Factory = Guid.Parse(factory.Value);
            return ui;
        }

    }
}
